package com.zy.eurekaconsumer;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MyController {     //功能代码---远程调用注册中心的服务

    //注入远程调用对象
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/consumer")
    @HystrixCommand(fallbackMethod = "hiFallback")
    public String getMesg(){

        //在这里测试远程调用制造失败，实现断路回调，提高容错机制
        System.out.println(1/0);

                                                    //远程调用地址，返回值类型
        String result=restTemplate.getForObject("http://eureka-client/client",String.class);

        System.out.println(result);
        return result;
    }

    //restTemplate在请求时是基于httpClient的请求


    public String hiFallback(){
        return "hi,调用失败，在这里实现断路回调hiFallback方法";
    }

}
